home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / stv.lha / STV / ISA / carolina / mdwsmltn.prj < prev    next >
Text File  |  1993-07-23  |  14KB  |  537 lines

  1. "
  2. ******************************************************************************
  3. Project : MeadowSimulation
  4. Date    : Jan 21, 1990
  5. Time    : 17:20:02
  6.  
  7. Classes : 
  8.     MeadowAnimal Rabbit Grass Fox Meadow MeadowInhabitant 
  9.  
  10. Methods : 
  11.  
  12. ******************************************************************************
  13. "!
  14.  
  15. Object subclass: #Meadow
  16.   instanceVariableNames: 
  17.     'foxes rabbits grasses date '
  18.   classVariableNames: ''
  19.   poolDictionaries: ''!
  20.  
  21. Object subclass: #MeadowInhabitant
  22.   instanceVariableNames: 
  23.     'age location displayState '
  24.   classVariableNames: ''
  25.   poolDictionaries: ''!
  26.  
  27. MeadowInhabitant subclass: #MeadowAnimal
  28.   instanceVariableNames: 
  29.     'lastAte lifeExpectancy birthRate foodRequirement lastBirth dailyRange '
  30.   classVariableNames: ''
  31.   poolDictionaries: ''!
  32.  
  33. MeadowAnimal subclass: #Rabbit
  34.   instanceVariableNames: 
  35.     'lastEaten '
  36.   classVariableNames: 
  37.     'RabbitIcon '
  38.   poolDictionaries: ''!
  39.  
  40. MeadowInhabitant subclass: #Grass
  41.   instanceVariableNames: 
  42.     'lastEaten '
  43.   classVariableNames: 
  44.     'GrassIcon '
  45.   poolDictionaries: ''!
  46.  
  47. MeadowAnimal subclass: #Fox
  48.   instanceVariableNames: ''
  49.   classVariableNames: 
  50.     'FoxIcon '
  51.   poolDictionaries: ''!
  52.  
  53.  
  54. !MeadowAnimal class methods !
  55.  
  56. example 
  57. " MeadowAnimal example " 
  58. "create instance of fox & rabbit" 
  59. "let them give birth and die" 
  60. | fox  | 
  61. fox := Fox new displayState: true; 
  62.             lastBirth: -2; 
  63.              birthRate: 1; 
  64.             lastAte: -2; 
  65.             lifeExpectancy: 20; 
  66.              foodRequirement: 1; 
  67.             age: 0. 
  68. fox birth ifTrue:[fox lastBirth: 0]. 
  69. fox death ifTrue:[fox displayState: false]. 
  70. fox inspect.! !
  71.  
  72.  
  73. !MeadowAnimal methods !
  74.  
  75. birth 
  76. "return true  if display status is true 
  77. and (age-lastBirth> birthRate) else false" 
  78. ^((self displayState) & 
  79. ((self age)-(self lastBirth) 
  80.             >(self birthRate)))!
  81.  
  82. birthRate: anInteger 
  83. "set the value for the animal's birth rate days/birth" 
  84. birthRate := anInteger!
  85.  
  86. death 
  87. "if (age> lifeExpectancy) 
  88. or age-lastAte>foodRequirement 
  89. or displayStatus = false 
  90. then return true, else false" 
  91. ^((self age > self lifeExpectancy) 
  92.  |((self age - self lastAte) > self foodRequirement) 
  93.  | self displayState not)!
  94.  
  95. distanceTo: anObject 
  96. "return the distance from self to anObject" 
  97. |point distance guess| 
  98. point := self location - anObject location. 
  99. distance := (point dotProduct: point)sqrt. 
  100. ^distance.!
  101.  
  102. lastAte 
  103. "return the day the animal last ate" 
  104. "default = 0" 
  105. lastAte isNil ifTrue:[self lastAte: 0]. 
  106. ^lastAte!
  107.  
  108. lastAte: anInteger 
  109. "set the age that animal last ate to anInteger" 
  110.  
  111. lastAte := anInteger!
  112.  
  113. lastBirth 
  114. "return the age of animal at the last birth" 
  115. "default = 0" 
  116. lastBirth isNil ifTrue: [self lastBirth: 0]. 
  117. ^lastBirth!
  118.  
  119. lastBirth: anInteger 
  120. "set the age of animal at the last birth" 
  121.  
  122. lastBirth := anInteger!
  123.  
  124. lifeExpectancy: anInteger 
  125. "set the value for the animal's life expectancy" 
  126. lifeExpectancy := anInteger!
  127.  
  128. locatePrey: aBag 
  129. "locate the nearest visible prey from a Bag" 
  130. "if distance to chosenPrey < dailyRange set prey displayState 
  131.  to false to indicate being eaten" 
  132. |distance minDistance capturedPrey| 
  133. minDistance := 999. "default" 
  134. capturedPrey := nil. "default" 
  135. aBag do:[:eachPrey | (eachPrey displayState) 
  136.     ifTrue: [distance := self distanceTo: eachPrey. 
  137.             (distance < minDistance) 
  138.         ifTrue: [minDistance := distance. 
  139.             capturedPrey := eachPrey]]]. 
  140. ^capturedPrey!
  141.  
  142. moveToPrey: anObject 
  143. "if distance < dailyRange change location to 
  144.  the location of the Prey and its displayState 
  145.  to false set captor's IV lastAte to its age" 
  146. "if distance > dailyRange change location to 
  147. location+(difference in locations)*dailyRange/distance" 
  148.  
  149. | distance | 
  150. (anObject isNil) ifTrue:[^nil]. 
  151. distance := (self location dotProduct: (anObject location)) sqrt. 
  152. (distance <= self dailyRange ) 
  153.     ifTrue:[self location: anObject location. 
  154.             anObject displayState: false. 
  155.             anObject lastEaten: anObject age. 
  156.              self lastAte: self age] 
  157.     ifFalse:[self location:(self location 
  158.         +(self location - anObject location)* 
  159.         (self dailyRange ) // distance)]! !
  160.  
  161.  
  162. !Rabbit class methods !
  163.  
  164. example
  165. "display Icon at location"
  166. " Rabbit example "
  167. |rabbit|
  168. rabbit := Rabbit new.
  169. self icon displayAt: 120@100.
  170. self inspect!
  171.  
  172. icon
  173. "return the RabbitIcon"
  174. RabbitIcon isNil ifTrue: [RabbitIcon := Form width: 16 height: 16
  175.      bitmap: #(63 252 63 248 31 242 95 246 79 230 103 206 119 222 55 220 55 220 176 13 176 5 167 241 140 153 204 153 239 123 239 123 )].
  176. ^RabbitIcon!
  177.  
  178. icon: aForm 
  179. "set the icon to be aForm" 
  180. RabbitIcon := aForm! !
  181.  
  182.  
  183. !Rabbit methods !
  184.  
  185. birthRate 
  186. "return the value of the birth rate" 
  187. "for a rabbit default = 1 per day" 
  188. birthRate isNil ifTrue: [self birthRate: 1]. 
  189. ^birthRate!
  190.  
  191. dailyRange 
  192. "return the daily range of species default 500" 
  193. dailyRange isNil ifTrue: [self dailyRange: 500]. 
  194. ^dailyRange!
  195.  
  196. dailyRange: anInteger 
  197. "set the daily Range to be anInteger" 
  198. dailyRange := anInteger.!
  199.  
  200. displayAt: aPoint 
  201. "display the receiver's icon at aPoint" 
  202. self icon displayAt: aPoint!
  203.  
  204. foodRequirement 
  205. "return the number of days the animal can live 
  206. without food default = 1" 
  207. foodRequirement isNil ifTrue: 
  208.     [self foodRequirement: 1]. 
  209. ^foodRequirement!
  210.  
  211. foodRequirement: anInteger 
  212. "set the number of days the animal can live 
  213. without food " 
  214.  foodRequirement := anInteger!
  215.  
  216. icon 
  217. "return the RabbitIcon" 
  218. ^RabbitIcon!
  219.  
  220. lastEaten: aValue 
  221.         "Set the value of lastEaten." 
  222.     lastEaten := aValue!
  223.  
  224. lifeExpectancy 
  225. "return the value of life expectancy" 
  226. "for a rabbit default = 10 days" 
  227. lifeExpectancy isNil 
  228.     ifTrue: [self lifeExpectancy: 10]. 
  229. ^lifeExpectancy! !
  230.  
  231.  
  232. !Grass class methods !
  233.  
  234. example
  235. "display Icon at location"
  236. " Grass example "
  237. |grass|
  238. grass := Grass new.
  239. self icon displayAt: 100@100.!
  240.  
  241. example1 
  242. "create instance of grass default displayState false 
  243. last eaten -3 age 0 and let it grow making 
  244. displayState = true" 
  245. " Grass example1 " 
  246. | gras | 
  247. gras := Grass new grow inspect!
  248.  
  249. icon
  250. "return the GrassIcon"
  251. GrassIcon isNil ifTrue: [GrassIcon :=  Form width: 16 height: 16
  252.  bitmap: #(253 255 237 239 237 239 237 238 238 238 238 238 110 221 110 217 110 219 118 215 182 214 214 182 214 180 192 3 128 1 0 0)].
  253. ^GrassIcon!
  254.  
  255. icon: aForm 
  256. "set the icon to be aForm" 
  257. GrassIcon := aForm! !
  258.  
  259.  
  260. !Grass methods !
  261.  
  262. displayAt: aPoint 
  263. "display the receiver's icon at aPoint" 
  264. self icon displayAt: aPoint!
  265.  
  266. grow 
  267. "if displayState is false and it was last 
  268. eaten > 2 days ago, set displayState to true" 
  269. ( self displayState not) & 
  270.     (( self age)-(self lastEaten) > 2) ifTrue: 
  271.         [self displayState: true]!
  272.  
  273. icon 
  274. "return the GrassIcon" 
  275. ^GrassIcon!
  276.  
  277. lastEaten 
  278. "return the number of days since grass was 
  279. last eaten default = -3" 
  280. lastEaten isNil ifTrue: [self lastEaten: -3]. 
  281. ^lastEaten!
  282.  
  283. lastEaten: anInteger 
  284. "set the date grass was last eaten to anInteger" 
  285. lastEaten := anInteger! !
  286.  
  287.  
  288. !Fox class methods !
  289.  
  290. example 
  291. "display Icon at location" 
  292. " Fox example " 
  293. |fox| 
  294. fox := Fox new. 
  295. self icon displayAt: 100@100.!
  296.  
  297. icon
  298. "return the FoxIcon"
  299. FoxIcon isNil ifTrue: [FoxIcon := Form width: 16 height: 16
  300.  bitmap: #(127 255 63 255 31 255 143 255 199 255 193 255 192 31 129 129 0 0 0 42 0 42 0 0 0 15 1 255 7 255 63 255 )].
  301. ^FoxIcon!
  302.  
  303. icon: aForm 
  304. "set the icon to be aForm" 
  305. FoxIcon := aForm! !
  306.  
  307.  
  308. !Fox methods !
  309.  
  310. birthRate 
  311. "return the value of the birth rate" 
  312. "for a fox default = 1 per 5 days" 
  313. birthRate isNil ifTrue: [self birthRate: 5]. 
  314. ^birthRate!
  315.  
  316. dailyRange 
  317. "return the daily range of species default 2000" 
  318.  dailyRange isNil ifTrue: [self dailyRange: 2000]. 
  319. ^dailyRange!
  320.  
  321. dailyRange: anInteger 
  322. "set the daily Range to be anInteger" 
  323. dailyRange := anInteger.!
  324.  
  325. displayAt: aPoint 
  326. "display the receiver's  icon at aPoint" 
  327. self icon displayAt: aPoint!
  328.  
  329. foodRequirement 
  330. "return the number of days the animal can live 
  331. without food default = 2" 
  332.  foodRequirement isNil ifTrue: 
  333.     [self foodRequirement: 2]. 
  334. ^foodRequirement!
  335.  
  336. foodRequirement: anInteger 
  337. "set the number of days the animal can live 
  338. without food " 
  339.  foodRequirement := anInteger!
  340.  
  341. icon 
  342. "return the FoxIcon" 
  343. ^FoxIcon!
  344.  
  345. lifeExpectancy 
  346. "return the value of life expectancy" 
  347. "for a fox default = 20 days" 
  348. lifeExpectancy isNil 
  349.     ifTrue: [self lifeExpectancy: 20]. 
  350. ^lifeExpectancy! !
  351.  
  352.  
  353. !Meadow class methods !
  354.  
  355. example 
  356. "create a meadow simulation for several days" 
  357. " Meadow example " 
  358. | days meadow | 
  359. meadow := Meadow initialize. 
  360. days := Prompter prompt: 'Run for how many days?' defaultExpression: '10'. 
  361. meadow runFor: days.!
  362.  
  363. initialize 
  364. "set up the meadow with 100 units of grass, 
  365. 25 rabbits and a fox" 
  366. |rand meadow grass rabbit fox| 
  367. rand := Random new. 
  368. meadow := Meadow new. 
  369. grass := Prompter prompt: 'How many patches of grass?' defaultExpression: '100'. 
  370. rabbit := Prompter prompt: 'How many rabbits?' defaultExpression: '20'. 
  371. fox := Prompter prompt: 'How many foxes?' defaultExpression: '2'. 
  372.  
  373. grass timesRepeat:[meadow grasses add: (Grass new 
  374.   location: (rand next:720)@(rand next:  340))]. 
  375.  
  376. rabbit timesRepeat:[meadow rabbits add: (Rabbit new 
  377.   location: (rand next:720)@(rand next:340); 
  378.     displayState: true; age: (rand next:5))]. 
  379.  
  380. fox timesRepeat:[meadow foxes add: (Fox new 
  381.     location: (rand next:720)@(rand next:340); 
  382.     displayState: true; age:(rand next: 10))]. 
  383. ^meadow.! !
  384.  
  385.  
  386. !Meadow methods !
  387.  
  388. date 
  389. "return the value of the current date 
  390. default is 0" 
  391. date isNil ifTrue: [self date: 0]. 
  392. ^date!
  393.  
  394. date: anInteger 
  395. "set the value of the current date to be anInteger" 
  396. date := anInteger!
  397.  
  398. display 
  399. "clear screen & display all inhabitants" 
  400. |field| 
  401. field := Form width: Display width height: (Display height - 10). 
  402. field displayAt: 0@10 rule: Form over. 
  403. 'Meadow simulation date:', date printString displayAt: 0@0. 
  404. 'Number of foxes:', foxes size printString displayAt: 200@0. 
  405. 'Number of rabbits:', rabbits size printString displayAt: 350@0. 
  406. self foxes do:[:each | (each displayState) ifTrue: [each displayAt: each location]]. 
  407. self rabbits do:[:each | (each displayState) ifTrue: [each displayAt: each location]]. 
  408. self grasses do:[:each | (each displayState) ifTrue: [each displayAt: each location]].!
  409.  
  410. foxes 
  411. "return the bag containing all foxes" 
  412. foxes isNil ifTrue: [self foxes: Bag new]. 
  413. ^foxes!
  414.  
  415. foxes: aBag 
  416. "set the bag containing all foxes" 
  417. foxes := aBag!
  418.  
  419. grasses 
  420. "return the bag containing all grasses" 
  421. grasses isNil ifTrue: [self grasses: Bag new]. 
  422. ^grasses!
  423.  
  424. grasses: aBag 
  425. "set the bag containing all grasses" 
  426. grasses := aBag.!
  427.  
  428. morning 
  429. "add 1 to date and to everyone's age" 
  430. self date: self date + 1. 
  431. self foxes do: [:each | each age: each age+1]. 
  432. self rabbits do:[:each | each age: each age+1]. 
  433. self grasses do:[:each | each age: each age+1].!
  434.  
  435. rabbits 
  436. "return the bag containing all rabbits" 
  437. rabbits isNil ifTrue: [self rabbits: Bag new]. 
  438. ^rabbits!
  439.  
  440. rabbits: aBag 
  441. "set the bag containing all rabbits" 
  442. rabbits := aBag!
  443.  
  444. runFor: nSteps 
  445. "run simulation for integer nSteps" 
  446. nSteps timesRepeat:[self runOneStep]!
  447.  
  448. runOneStep  |rand| 
  449. rand := Random new. 
  450. "run simulation one step" 
  451. "add 1 to everyone's age" 
  452. self morning. 
  453. "let grass grow" 
  454. self grasses do:[:grass | grass grow]. 
  455. "let rabbits locate grass, move to it." 
  456. self rabbits do:[:rabbit | rabbit moveToPrey: (rabbit locatePrey: grasses)]. 
  457. "let foxes locate rabbits, move towards them" 
  458. self foxes do:[:fox | fox moveToPrey: (fox locatePrey: rabbits)]. 
  459. "let rabbits die" 
  460. self rabbits do:[:rabbit | (rabbit death) 
  461.     ifTrue:[rabbits remove: rabbit ifAbsent: nil ]]. 
  462. "let foxes die" 
  463. self foxes do:[:fox | (fox death) 
  464.     ifTrue:[foxes remove: fox ifAbsent: nil ]]. 
  465. "let rabbits give birth." 
  466. self rabbits do:[:rabbit | (rabbit birth) 
  467.     ifTrue:[rabbit lastBirth: rabbit age. 
  468.          rabbits add:(Rabbit new location: rabbit location)]]. 
  469. "let foxes give birth" 
  470. self foxes do:[:fox | (fox birth) 
  471.     ifTrue:[ fox lastBirth: fox age. 
  472.         foxes add:(Fox new location: fox location)]]. 
  473.  
  474. "display final state" 
  475. self display! !
  476.  
  477.  
  478. !MeadowInhabitant class methods !
  479.  
  480. example1 
  481. "create and display various MeadowInhabitants" 
  482. " MeadowInhabitant example1 " 
  483. |rand meadow | 
  484. rand := Random new. 
  485. meadow := Meadow new. 
  486. meadow grasses. 
  487. meadow rabbits. 
  488. meadow foxes. 
  489. 25 timesRepeat:[meadow grasses add: (Grass new displayAt:(rand next:720)@(rand next:340))]. 
  490. 5 timesRepeat:[meadow rabbits add: (Rabbit new displayAt:(rand next:720)@(rand next:340))]. 
  491. meadow foxes add: (Fox new displayAt:(rand next:720)@(rand next:340)).! !
  492.  
  493.  
  494. !MeadowInhabitant methods !
  495.  
  496. age 
  497. "return the value of age default = 0" 
  498. age isNil ifTrue: [self age: 0]. 
  499. ^age!
  500.  
  501. age: anInteger 
  502. "set the value of age to anInteger " 
  503. age := anInteger!
  504.  
  505. displayState 
  506. "return the value of displayState default true" 
  507. displayState isNil ifTrue:[self displayState: true]. 
  508. ^displayState!
  509.  
  510. displayState: aBoolean 
  511. "set the value of displayState to aBoolean" 
  512. displayState := aBoolean!
  513.  
  514. location 
  515. "return the value of location default 0@0" 
  516. location isNil ifTrue: [self location: 0@0]. 
  517. ^location!
  518.  
  519. location: aPoint 
  520. "set the value of location to aPoint" 
  521. location := aPoint min: (720@340).! !
  522.  
  523. "construct application" 
  524. ((Smalltalk at: #Application ifAbsent: []) 
  525.     isKindOf: Class) ifTrue: [ 
  526.         ((Smalltalk at: #Application) for:'MeadowSimulation')
  527.             addClass: MeadowAnimal;
  528.             addClass: Rabbit;
  529.             addClass: Grass;
  530.             addClass: Fox;
  531.             addClass: Meadow;
  532.             addClass: MeadowInhabitant;
  533.             comments: nil;
  534.             initCode: nil;
  535.             finalizeCode: nil;
  536.             startUpCode: nil]!
  537.